package com.powerbank.usercenter.controller;

import com.powerbank.common.result.Result;
import com.powerbank.common.result.PageResult;
import com.powerbank.common.query.PageQuery;
import com.powerbank.usercenter.dto.UserRegisterDTO;
import com.powerbank.usercenter.dto.UserLoginDTO;
import com.powerbank.usercenter.dto.UserUpdateDTO;
import com.powerbank.usercenter.service.UserService;
import com.powerbank.usercenter.vo.UserInfoVO;
import com.powerbank.usercenter.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

/**
 * 用户管理控制器
 */
@Tag(name = "用户管理", description = "用户注册、登录、信息管理等接口")
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "新用户注册接口")
    @PostMapping("/register")
    public Result<UserLoginVO> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        log.info("用户注册请求: phone={}", registerDTO.getPhone());
        UserLoginVO loginVO = userService.register(registerDTO);
        return Result.success(loginVO);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "用户登录接口")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        log.info("用户登录请求: phone={}", loginDTO.getPhone());
        UserLoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO);
    }

    /**
     * 退出登录
     */
    @Operation(summary = "退出登录", description = "用户退出登录接口")
    @PostMapping("/logout")
    public Result<Void> logout(@RequestHeader("Authorization") String token) {
        log.info("用户退出登录");
        userService.logout(token);
        return Result.success();
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    @GetMapping("/{userId}")
    public Result<UserInfoVO> getUserInfo(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        log.info("获取用户信息: userId={}", userId);
        UserInfoVO userInfo = userService.getUserInfo(userId);
        return Result.success(userInfo);
    }

    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "更新用户基本信息")
    @PutMapping("/{userId}")
    public Result<Void> updateUserInfo(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId,
            @Valid @RequestBody UserUpdateDTO updateDTO) {
        log.info("更新用户信息: userId={}", userId);
        userService.updateUserInfo(userId, updateDTO);
        return Result.success();
    }

    /**
     * 获取当前用户信息
     */
    @Operation(summary = "获取当前用户信息", description = "根据Token获取当前登录用户信息")
    @GetMapping("/current")
    public Result<UserInfoVO> getCurrentUserInfo(@RequestHeader("X-User-Id") Long userId) {
        log.info("获取当前用户信息: userId={}", userId);
        UserInfoVO userInfo = userService.getUserInfo(userId);
        return Result.success(userInfo);
    }

    /**
     * 修改密码
     */
    @Operation(summary = "修改密码", description = "用户修改登录密码")
    @PutMapping("/{userId}/password")
    public Result<Void> changePassword(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId,
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword) {
        log.info("用户修改密码: userId={}", userId);
        userService.changePassword(userId, oldPassword, newPassword);
        return Result.success();
    }

    /**
     * 绑定手机号
     */
    @Operation(summary = "绑定手机号", description = "绑定或更换手机号")
    @PutMapping("/{userId}/phone")
    public Result<Void> bindPhone(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId,
            @RequestParam("phone") String phone,
            @RequestParam("smsCode") String smsCode) {
        log.info("用户绑定手机号: userId={}, phone={}", userId, phone);
        userService.bindPhone(userId, phone, smsCode);
        return Result.success();
    }

    /**
     * 分页查询用户列表（管理员接口）
     */
    @Operation(summary = "分页查询用户列表", description = "管理员查询用户列表接口")
    @GetMapping("/page")
    public Result<PageResult<UserInfoVO>> getUserPage(PageQuery pageQuery,
                                                     @RequestParam(required = false) String keyword) {
        log.info("分页查询用户列表: page={}, size={}, keyword={}", 
                pageQuery.getPage(), pageQuery.getSize(), keyword);
        PageResult<UserInfoVO> pageResult = userService.getUserPage(pageQuery, keyword);
        return Result.success(pageResult);
    }

    /**
     * 禁用用户
     */
    @Operation(summary = "禁用用户", description = "管理员禁用用户账号")
    @PutMapping("/{userId}/disable")
    public Result<Void> disableUser(@Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        log.info("禁用用户: userId={}", userId);
        userService.disableUser(userId);
        return Result.success();
    }

    /**
     * 启用用户
     */
    @Operation(summary = "启用用户", description = "管理员启用用户账号")
    @PutMapping("/{userId}/enable")
    public Result<Void> enableUser(@Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        log.info("启用用户: userId={}", userId);
        userService.enableUser(userId);
        return Result.success();
    }
}